package com.waikuai.shop.controller;

import com.waikuai.shop.common.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/notices")
@RequiredArgsConstructor
public class NoticeController {

    @GetMapping
    public Result<List<Map<String, Object>>> getNoticeList() {
        try {
            List<Map<String, Object>> notices = new ArrayList<>();
            
            Map<String, Object> notice1 = new HashMap<>();
            notice1.put("id", 1L);
            notice1.put("title", "员工福利商城正式上线");
            notice1.put("content", "欢迎使用员工福利商城，您可以通过福利卡兑换各类商品。");
            notice1.put("type", "system");
            notice1.put("createTime", "2024-10-01 10:00:00");
            notices.add(notice1);
            
            Map<String, Object> notice2 = new HashMap<>();
            notice2.put("id", 2L);
            notice2.put("title", "本月新品上架通知");
            notice2.put("content", "本月新增50+精选商品，欢迎选购。");
            notice2.put("type", "product");
            notice2.put("createTime", "2024-10-15 14:00:00");
            notices.add(notice2);
            
            Map<String, Object> notice3 = new HashMap<>();
            notice3.put("id", 3L);
            notice3.put("title", "年度积分即将清零提醒");
            notice3.put("content", "本年度福利额度将于12月31日清零，请及时使用。");
            notice3.put("type", "warning");
            notice3.put("createTime", "2024-10-10 09:00:00");
            notices.add(notice3);
            
            log.info("获取公告列表成功，数量: {}", notices.size());
            return Result.success("获取公告列表成功", notices);
        } catch (Exception e) {
            log.error("获取公告列表失败: {}", e.getMessage(), e);
            return Result.error("获取公告列表失败");
        }
    }

    @GetMapping("/system")
    public Result<List<Map<String, Object>>> getSystemNotices() {
        try {
            List<Map<String, Object>> notices = new ArrayList<>();
            
            Map<String, Object> notice1 = new HashMap<>();
            notice1.put("id", 1L);
            notice1.put("title", "员工福利商城正式上线");
            notice1.put("content", "欢迎使用员工福利商城，您可以通过福利卡兑换各类商品。");
            notice1.put("type", "system");
            notice1.put("createTime", "2024-10-01 10:00:00");
            notices.add(notice1);
            
            Map<String, Object> notice3 = new HashMap<>();
            notice3.put("id", 3L);
            notice3.put("title", "年度积分即将清零提醒");
            notice3.put("content", "本年度福利额度将于12月31日清零，请及时使用。");
            notice3.put("type", "warning");
            notice3.put("createTime", "2024-10-10 09:00:00");
            notices.add(notice3);
            
            log.info("获取系统公告成功，数量: {}", notices.size());
            return Result.success("获取系统公告成功", notices);
        } catch (Exception e) {
            log.error("获取系统公告失败: {}", e.getMessage(), e);
            return Result.error("获取系统公告失败");
        }
    }

    @GetMapping("/latest")
    public Result<List<Map<String, Object>>> getLatestNotices() {
        try {
            List<Map<String, Object>> notices = new ArrayList<>();
            
            Map<String, Object> notice2 = new HashMap<>();
            notice2.put("id", 2L);
            notice2.put("title", "本月新品上架通知");
            notice2.put("content", "本月新增50+精选商品，欢迎选购。");
            notice2.put("type", "product");
            notice2.put("createTime", "2024-10-15 14:00:00");
            notices.add(notice2);
            
            log.info("获取最新公告成功，数量: {}", notices.size());
            return Result.success("获取最新公告成功", notices);
        } catch (Exception e) {
            log.error("获取最新公告失败: {}", e.getMessage(), e);
            return Result.error("获取最新公告失败");
        }
    }

    @GetMapping("/{id}")
    public Result<Map<String, Object>> getNoticeDetail(@PathVariable Long id) {
        try {
            Map<String, Object> notice = new HashMap<>();
            notice.put("id", id);
            notice.put("title", "公告标题");
            notice.put("content", "公告详细内容");
            notice.put("type", "system");
            notice.put("createTime", "2024-10-01 10:00:00");
            
            log.info("获取公告详情成功，公告ID: {}", id);
            return Result.success("获取公告详情成功", notice);
        } catch (Exception e) {
            log.error("获取公告详情失败，公告ID: {}, 错误: {}", id, e.getMessage(), e);
            return Result.error("获取公告详情失败");
        }
    }

    @PutMapping("/{id}/read")
    public Result<String> markNoticeAsRead(@PathVariable Long id) {
        try {
            log.info("标记公告为已读成功，公告ID: {}", id);
            return Result.success("标记已读成功");
        } catch (Exception e) {
            log.error("标记公告为已读失败，公告ID: {}, 错误: {}", id, e.getMessage(), e);
            return Result.error("标记已读失败");
        }
    }
}
